/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dyniablaster.graphics;

import dyniablaster.logic.DyniaThread;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.m2g.SVGImage;
import org.netbeans.microedition.svg.SVGMenu;
import org.netbeans.microedition.svg.SVGSplashScreen;

/**
 * @author Damian2
 */
public class DyniaBlasterVisualMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;

    //glowny watek gry
    private DyniaThread dyniaGameThread;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private SVGImage svgImage1;
    private SVGImage svgImage;
    private SVGSplashScreen svgSplashScreen;
    private DyniaBlasterCanvas dyniaBlasterCanvas;
    private SVGMenu svgMenu;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The DyniaBlasterVisualMIDlet constructor.
     */
    public DyniaBlasterVisualMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getSvgMenu().getSvgCanvas());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
       // dyniaGameThread = new DyniaThread(getDyniaBlasterCanvas(), this);
       // dyniaGameThread.start();

    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: dyniaBlasterCanvas ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initiliazed instance of dyniaBlasterCanvas component.
     * @return the initialized component instance
     */
    public DyniaBlasterCanvas getDyniaBlasterCanvas() {
        if (dyniaBlasterCanvas == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            dyniaBlasterCanvas = new DyniaBlasterCanvas(this);//GEN-BEGIN:|18-getter|1|18-postInit
            dyniaBlasterCanvas.setTitle("dyniaBlasterCanvas");//GEN-END:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return dyniaBlasterCanvas;
    }
    //</editor-fold>//GEN-END:|18-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == svgMenu) {//GEN-BEGIN:|7-commandAction|1|22-preAction
            if (command == SVGMenu.SELECT_COMMAND) {//GEN-END:|7-commandAction|1|22-preAction
                // write pre-action user code here
                svgMenuAction();//GEN-LINE:|7-commandAction|2|22-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|36-preAction
        } else if (displayable == svgSplashScreen) {
            if (command == SVGSplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|3|36-preAction
                // write pre-action user code here
                switchDisplayable(null, getSvgMenu().getSvgCanvas());//GEN-LINE:|7-commandAction|4|36-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
        }//GEN-END:|7-commandAction|5|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|6|
    //</editor-fold>//GEN-END:|7-commandAction|6|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgMenu ">//GEN-BEGIN:|20-getter|0|20-preInit
    /**
     * Returns an initiliazed instance of svgMenu component.
     * @return the initialized component instance
     */
    public SVGMenu getSvgMenu() {
        if (svgMenu == null) {//GEN-END:|20-getter|0|20-preInit
            // write pre-init user code here
            svgMenu = new SVGMenu(getSvgImage1(), getDisplay());//GEN-BEGIN:|20-getter|1|20-postInit
            svgMenu.setTitle("svgMenu");
            svgMenu.addCommand(SVGMenu.SELECT_COMMAND);
            svgMenu.setCommandListener(this);
            svgMenu.addMenuElement("menuItem_0");
            svgMenu.addMenuElement("menuItem_1");
            svgMenu.addMenuElement("menuItem_2");//GEN-END:|20-getter|1|20-postInit
            // write post-init user code here
        }//GEN-BEGIN:|20-getter|2|
        return svgMenu;
    }
    //</editor-fold>//GEN-END:|20-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: svgMenuAction ">//GEN-BEGIN:|20-action|0|20-preAction
    /**
     * Performs an action assigned to the selected SVG menu element in the svgMenu component.
     */
    public void svgMenuAction() {//GEN-END:|20-action|0|20-preAction
        // enter pre-action user code here
        String __selectedElement = getSvgMenu().getMenuElementID(getSvgMenu().getSelectedIndex());//GEN-BEGIN:|20-action|1|24-preAction
        if (__selectedElement != null) {
            if (__selectedElement.equals("menuItem_0")) {//GEN-END:|20-action|1|24-preAction
                // write pre-action user code here
                 dyniaGameThread = new DyniaThread(getDyniaBlasterCanvas(), this);
                dyniaGameThread.start();

                switchDisplayable(null, getDyniaBlasterCanvas());//GEN-LINE:|20-action|2|24-postAction
                // write post-action user code here
            } else if (__selectedElement.equals("menuItem_1")) {//GEN-LINE:|20-action|3|25-preAction
                // write pre-action user code here
                switchDisplayable(null, getSvgSplashScreen().getSvgCanvas());//GEN-LINE:|20-action|4|25-postAction
                // write post-action user code here
            } else if (__selectedElement.equals("menuItem_2")) {//GEN-LINE:|20-action|5|26-preAction
                // write pre-action user code here
                exitMIDlet();
//GEN-LINE:|20-action|6|26-postAction
                // write post-action user code here
            }//GEN-BEGIN:|20-action|7|20-postAction
        }//GEN-END:|20-action|7|20-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|20-action|8|
    //</editor-fold>//GEN-END:|20-action|8|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgImage1 ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of svgImage1 component.
     * @return the initialized component instance
     */
    public SVGImage getSvgImage1() {
        if (svgImage1 == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|33-getter|1|33-@java.io.IOException
                svgImage1 = (SVGImage) SVGImage.createImage(getClass().getResourceAsStream("/menu.svg"), null);
            } catch (java.io.IOException e) {//GEN-END:|33-getter|1|33-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|33-getter|2|33-postInit
            // write post-init user code here
        }//GEN-BEGIN:|33-getter|3|
        return svgImage1;
    }
    //</editor-fold>//GEN-END:|33-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgImage ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of svgImage component.
     * @return the initialized component instance
     */
    public SVGImage getSvgImage() {
        if (svgImage == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|38-getter|1|38-@java.io.IOException
                svgImage = (SVGImage) SVGImage.createImage(getClass().getResourceAsStream("/instrukcja.svg"), null);
            } catch (java.io.IOException e) {//GEN-END:|38-getter|1|38-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|38-getter|2|38-postInit
            // write post-init user code here
        }//GEN-BEGIN:|38-getter|3|
        return svgImage;
    }
    //</editor-fold>//GEN-END:|38-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: svgSplashScreen ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initiliazed instance of svgSplashScreen component.
     * @return the initialized component instance
     */
    public SVGSplashScreen getSvgSplashScreen() {
        if (svgSplashScreen == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            svgSplashScreen = new SVGSplashScreen(getSvgImage(), getDisplay());//GEN-BEGIN:|34-getter|1|34-postInit
            svgSplashScreen.setTitle("svgSplashScreen");
            svgSplashScreen.setCommandListener(this);//GEN-END:|34-getter|1|34-postInit
            // write post-init user code here
        }//GEN-BEGIN:|34-getter|2|
        return svgSplashScreen;
    }
    //</editor-fold>//GEN-END:|34-getter|2|






    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
